//
//  DOUOAuthRequest.m
//  DOUAPIEngine
//
//  Created by Lin GUO on 11-10-31.
//  Copyright (c) 2011年 Douban Inc. All rights reserved.
//

#import "DOUOAuth2Provider.h"
#import "DOUOAuth2.h"
#import "DOUHttpRequest.h"

@interface DOUOAuth2Provider ()
@end

@implementation DOUOAuth2Provider

static NSUInteger kErrorCodeAccessTokenHasExpired = 106;


- (id)initWithAuthURL:(NSString *)anAuthURL tokenURL:(NSString *)aTokenURL {
  self = [super init];
  if (self) {
    authURL_ = [anAuthURL retain];
    tokenURL_ = [aTokenURL retain];    
  }
  return self;
}


- (void)dealloc {
  [authURL_ release]; authURL_ = nil;
  [tokenURL_ release]; tokenURL_ = nil;
	[super dealloc];
}


- (void)accessTokenWithConsumer:(DOUOAuth2Consumer *)consumer andRequest:(DOUHttpRequest *)request {
  
  NSLog(@"auth %@", [request url]);
  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
    
    if ([request responseStatusCode] == kErrorCodeAccessTokenHasExpired) {
      [self accessTokenByRefresh:consumer];
    }
    
    NSString* responseStr = [request responseString];
    [consumer updateWithHTTPResponse:responseStr];
    [consumer save];
  }
  
}


- (void)accessTokenByPassword:(DOUOAuth2Consumer *)consumer 
                     username:(NSString *)username 
                  andPassword:(NSString *)password {

  DOUHttpRequest *req = [DOUHttpRequest requestWithURL:[NSURL URLWithString:tokenURL_]];
  [req setRequestMethod:@"POST"];
  [req setPostValue:consumer.key forKey:kClientIdKey];
  [req setPostValue:consumer.secret forKey:kClientSecretKey];
  [req setPostValue:kGrantTypePassword forKey:kGrantTypeKey];
  [req setPostValue:username forKey:kUsernameKey];
  [req setPostValue:password forKey:kPasswordKey];
  [req setStringEncoding:NSUTF8StringEncoding];
  
  [self accessTokenWithConsumer:consumer andRequest:req];
} 

 

- (void)accessTokenByRefresh:(DOUOAuth2Consumer *)consumer {
  DOUHttpRequest *req = [DOUHttpRequest requestWithURL:[NSURL URLWithString:tokenURL_]];
  [req setRequestMethod:@"POST"];
  [req setPostValue:consumer.key forKey:kClientIdKey];
  [req setPostValue:consumer.secret forKey:kClientSecretKey];
  [req setPostValue:kGrantTypeRefreshToken forKey:kGrantTypeKey];  
  [req setPostValue:consumer.refreshToken forKey:kRefreshTokenKey];
  
  [req setStringEncoding:NSUTF8StringEncoding];
  [self accessTokenWithConsumer:consumer andRequest:req];
}

/*
- (void)accessTokenByAuthorizationCode:(DOUOAuth2Consumer *)consumer 
                     authorizationCode:(NSString *)authorizationCode {
  DOUHttpRequest *req = [DOUHttpRequest requestWithURL:[NSURL URLWithString:tokenURL_]];
  [req setRequestMethod:@"POST"];
  [req setPostValue:consumer.key forKey:kClientIdKey];
  [req setPostValue:consumer.secret forKey:kClientSecretKey];
  [req setPostValue:consumer.redirectURL forKey:kRedirectURIKey];
  
  [req setPostValue:kGrantTypeAuthorizationCode forKey:kGrantTypeKey];  
  [req setPostValue:authorizationCode forKey:kOAuth2ResponseTypeCode];
  
  [req setStringEncoding:NSUTF8StringEncoding];
  [self accessTokenWithConsumer:consumer andRequest:req];
}
*/



@end
